//
//  HeroViewController.m
//  Hero
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "HeroViewController.h"
#import "HeroModel.h"
#import "HeroCollectionViewCell.h"
#import "CaidanView.h"
@interface HeroViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,SelectDelegate>
@property (nonatomic, strong) NSMutableArray *allListArray;
@property (nonatomic, strong) UICollectionView *conllectionView;
@property (nonatomic, strong) CaidanView *caidanView;
@property (nonatomic, strong) NSMutableArray *allListArray2;

@end

@implementation HeroViewController
static bool isShow = NO;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.allListArray2 = [NSMutableArray new];
    self.view.backgroundColor = [UIColor whiteColor];
    self.allListArray = [NSMutableArray new];
    [self reloadDate];
    UICollectionViewFlowLayout *layOut = [UICollectionViewFlowLayout new];
    layOut.itemSize = CGSizeMake(160, 90);
    layOut.minimumInteritemSpacing = 20;
    layOut.minimumLineSpacing = 20;
    self.conllectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layOut];
    [self.view addSubview:self.conllectionView];
    self.conllectionView.delegate = self;
    self.conllectionView.backgroundColor = [UIColor whiteColor];
    self.conllectionView.dataSource = self;
    [self.conllectionView registerNib:[UINib nibWithNibName:@"HeroCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    self.caidanView = [[CaidanView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.caidanView];
    self.caidanView.myDelegate = self;
}
//点击位置
- (IBAction)didClickWeizhiButtonAction:(UIButton *)sender {
    if (isShow) {
        [UIView animateWithDuration:1 animations:^{
            self.caidanView.frame = CGRectZero;
          
        }];
        isShow = NO;
    }
    else{
        [UIView animateWithDuration:1 animations:^{
            self.caidanView.frame = CGRectMake(95, 100, 100, 100);
            
        }];
        isShow = YES;
    }
    
}
//代理方法的实现
- (void)didSelectCellWith:(NSString *)weizhi
{
    UIButton *button = [self.view viewWithTag:2000];
    [button setTitle:weizhi forState:UIControlStateNormal];
    [UIView animateWithDuration:1 animations:^{
        self.caidanView.frame = CGRectZero;
        
    }];
    isShow = NO;
    NSMutableArray *array = [NSMutableArray new];
    if ([weizhi isEqualToString:@"全部"]) {
        [self.allListArray addObjectsFromArray:self.allListArray2];
        return;
    }else{
       
    for (HeroModel *model in self.allListArray2) {
//       NSLog(@"%@",model.tag1);
        if ([model.tag1 isEqualToString:weizhi ] || [model.tag2 isEqualToString:weizhi] || [model.tag3 isEqualToString:weizhi]) {
            [array addObject:model];
//             NSLog(@"%@",array);
        }
       
    }
//     NSLog(@"%@",array);
    [self.allListArray removeAllObjects];
    [self.allListArray addObjectsFromArray:array];
    [self.conllectionView reloadData];
    }
}
//点击价格
- (IBAction)didClickButtonAction:(UIButton *)sender {
}
- (void)reloadDate
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://ossweb-img.qq.com/upload/qqtalk/lol_hero/hero_list.js"] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//             NSLog(@"%@",array);
            for (NSDictionary *dict in array) {
                HeroModel *model = [[HeroModel alloc]init];
//                 NSLog(@"%@",dict);
                [model setValuesForKeysWithDictionary:dict];
                [self.allListArray addObject:model];
            }
            [self.allListArray2 addObjectsFromArray:self.allListArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.conllectionView reloadData];
            });
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.allListArray[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allListArray.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
